<template>
  <el-card class="station-emp-charts">
    <div class="charts" ref="stationEmpCharts"></div>
  </el-card>
</template>

<script>
import { statisticsStationEmpApi } from "@/api/statistics";
export default {
  name: "StationEmpCharts",
  data() {
    return {
      data: [],
      keys: [],
    };
  },
  mounted() {
    this.getStatisticsData();
  },
  created() {},
  methods: {
    async getStatisticsData() {
      const myCharts = this.$echarts.init(this.$refs.stationEmpCharts);
      const { code, data } = await statisticsStationEmpApi();
      if (code !== 200) return;
      this.data = data;
      this.keys = this.data.map((item) => item.name);
      let total = 0;
      this.data.forEach((item) => {
        total += item.value;
      });
      const option = {
        title: {
          text: `公司各岗位人数统计情况`,
          subtext: `总共${total}人`,
          left: "center",
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b}: {c} ({d}%)",
        },
        legend: {
          orient: "vertical",
          left: 10,
          data: this.keys,
        },
        series: [
          {
            name: "岗位在职人数",
            type: "pie",
            radius: ["50%", "70%"],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "30",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: this.data,
            itemStyle: {
              color: function (params) {
                var colorList = [
                  "#2980b9",
                  "#8e44ad",
                  "#27ae60",
                  "#FD7272",
                  "#e74c3c",
                  "#45aaf2",
                  "#2c3e50",
                  "#0fb9b1",
                  "#e74c3c",
                  "#e74c3c",
                  "#FD7272",
                  "#45aaf2",
                  "#2c3e50",
                  "#0fb9b1",
                ];
                return colorList[params.dataIndex];
              },
            },
          },
        ],
      };

      myCharts.clear();
      myCharts.setOption(option);
    },
  },
};
</script>

<style scoped lang="scss">
.charts {
  margin-top: 45px;
  height: 300px;
}
.station-emp-charts {
  margin-top: 30px;
}
</style>
